package com.sky.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.sky.utils.AliyunOssProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

/**
 * 对应AliyunOssOperator的配置类
 *
 * @Author mr.zhang
 * Date 2025/5/21 1:51
 */
@Configuration
public class AliyunOssConfiguration {

    @Autowired
    private AliyunOssProperties aliyunOssProperties;

    private OSS ossClient;//创建OSS实例

    @Bean
    public OSS getOssClient(){
        return new OSSClientBuilder().build(
                aliyunOssProperties.getEndpoint(),
                aliyunOssProperties.getAccessKeyId(),
                aliyunOssProperties.getAccessKeySecret()
        );
    }

    /**
     * @PreDestroy是spring提供的注解,用于在Bean被销毁前执行清除操作
     */
    @PreDestroy
    public void destoryOssClient(){
        if (ossClient!=null){
            ossClient.shutdown();
        }
    }
}
